package com.my.uitl;

import com.my.javabean.User;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class UserMapperTest {
    SqlSession session;
    UserMapper mapper;

    @Before
    public void before() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
        mapper = session.getMapper(UserMapper.class);

    }

    @After
    public void after() {
        //5、关闭资源
        session.close();
    }
    @Test
    public void selectAll(){
        for (User user : mapper.selectAll()) {
            System.out.println(user);
        }
    }
    @Test
    public void selectByUsernameAndPassword(){
        for (User user : mapper.selectByUsernameAndPassword("a","a")) {
            System.out.println(user);
        }
    }
    @Test
    public void selectByUid(){
        mapper.selectByUid(1);
    }
    @Test
    public void insertUser(){
        User u=new User("a","a","a");
        mapper.insertUser(u);
    }

    @Test
    public void selectUser(){
        for (User user : mapper.selectUser()) {
            System.out.println(user);
        }
    }
    @Test
    public void selectUser1(){
        for (User user : mapper.selectUser1()) {
            System.out.println(user);
        }
    }
    @Test
    public void selectUser_Role(){
        for (User user : mapper.selectUser_Role()) {
            System.out.println(user);
        }
    }
    @Test
    public void selectByUser(){
        User u=new User();
        u.setUid(1);
        u.setPassword("a");
        for (User user : mapper.selectByUser(u)) {
            System.out.println(user);
        }
    }
}